library(ggplot2)
library(reshape2)
library(dplyr)
library(ggthemes)
library(tidyr)
library(tidyverse)
library(ggpubr)

# 1. Basic barplot ----

## 1.1 One-variable (discrete variable)
ggplot(data = diamonds) +
    geom_bar(aes(x = cut))

# Have same effect as the above code
ggplot(data = diamonds) +
    stat_count(aes(x = cut))

# Display the proportion [..prop..]
ggplot(diamonds) +
    geom_bar(aes(x = cut, y = ..prop.., group = 1))

# geom_bar() plot the counting (default)
# geom_col() is used to plot the original data (without counting)

# 1.2 Two-variable (discrete X, continous Y)
demo <- tribble(~ a,     ~ b,
                "bar1", 20,
                "bar2", 30,
                "bar3", 40)

ggplot(demo) +
    geom_bar(aes(x = a, y = b), stat = "identity") + 
    labs(title = "Bar plot", x = "Discrete X", y = "Continous Y")

# Have same effect as the above code
ggplot(demo) +
    geom_col(aes(x = a, y = b))

# Bar plot by ggpubr
ggbarplot(demo, x = "a", y = "b",
          label = TRUE,
          color = "steelblue", fill = "steelblue",
          title = "Bar plot (ggpubr)", 
          xlab = "Discrete X", ylab = "Continous Y",
          ggtheme = theme_pubr())   # theme_gray()

# ggbarplot(diamonds, x = "cut", y = "cut", add = "length")

# ggbarplot() is NOT suit to draw one-variable barplot.
# Actually, if you want plot the counting bar,
#   you should calculate the count manually.
gg.diamonds <- diamonds %>% 
    group_by(cut) %>% 
    summarise(n = n())
gg.diamonds <- count(diamonds, cut)
ggbarplot(gg.diamonds, x = "cut", y = "n",
          fill = "steelblue", color = "steelblue",
          orientation = "horizontal",
          sort.val = "desc",
          theme = theme_gray())

# Plot the histogram
gghistogram(diamonds, x = "cut")


# 2. ggplot2 and ggbarplot ----
df <- data.frame(dose = c("D0.5", "D1", "D2"),
                 len = c(4.2, 10, 29.5))
head(df)

## 2.1. Barplot (discrete X, continous Y)
p <- ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity")
# width is 90% of screen resolution (default)
p

ggbarplot(df, x = "dose", y = "len") +
    theme_gray()

ggbarplot(df, x = "dose", y = "len", width = 0.9,
          ggtheme = theme_gray())

### 2.1.1. Rotate to horizontal barplot
p + coord_flip()
# Have same effect of rotation as above code
ggbarplot(df, x = "dose", y = "len", rotate = TRUE)
ggbarplot(df, x = "dose", y = "len") +
    theme_gray() + rotate()

### 2.2.2. Figure properties
ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", width = 0.5)
ggbarplot(df, x = "dose", y = "len", 
          width = 0.5, fill = "gray",
          ggtheme = theme_gray())

### 2.2.3. Change fill color and line color
ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", 
    color = "steelblue", fill = "white")
ggbarplot(df, x = "dose", y = "len", 
          color = "steelblue", fill = "white",
          ggtheme = theme_gray())

### 2.2.4. Set labels of X and Y axis
ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", fill = "steelblue") +
    labs(x = "Dose", y = "Length")
ggbarplot(df, x = "dose", y = "len", fill = "steelblue",
          xlab = "Dose", ylab = "Length",
    ggtheme = theme_gray())

### 2.2.5. Set theme
p <- ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", fill = "steelblue") +
    theme_minimal()
p
ggbarplot(df, x = "dose", y = "len", 
          fill = "steelblue", color = NA,
          xlab = "Dose", ylab = "Length",
          ggtheme = theme_minimal())

### 2.2.6. Select part columns to plot
p + scale_x_discrete(limits = c("D0.5", "D2"))
ggbarplot(df, x = "dose", y = "len",
          select = c("D0.5", "D2"))

### 2.2.7. Data label
# Outside label
ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", fill = "steelblue") +
    geom_text(aes(label = len), vjust = -0.3, size = 3.5) +
    theme_minimal()
ggbarplot(df, x = "dose", y = "len",
          fill = "steelblue", color = NA,
          label = "len", lab.size = 3.5, lab.vjust = -0.3,
          ggtheme = theme_minimal())

# Inner label
ggplot(df, aes(dose, len)) +
    geom_bar(stat = "identity", fill = "steelblue") +
    geom_text(aes(label = len), vjust = 1.6, color = "white", size = 3.5) +
    theme_minimal()
ggbarplot(df, x = "dose", y = "len",
          fill = "steelblue", color = NA,
          label = "len", lab.vjust = 1.6, lab.col = "white", lab.size = 3.5,
          ggtheme = theme_minimal())

## 2.2. Counting barplot (discrete X) ----
# stat = "identity", draw barplot with original data
# stat = "count",  draw barplot with counting data (default)
#   for 'factor' variable counting
head(mtcars)
ggplot(mtcars, aes(x = factor(cyl))) +
    geom_bar(stat = "count", width = 0.7, fill = "steelblue") +
    theme_minimal()

# Create counting data.frame, manually.
df2 <- mtcars %>% group_by(cyl) %>% summarise(n_cyl = n())
df2$cyl <- as.factor(df2$cyl)
# ggpubr is NOT like ggplot2: NO counting, 
#   ONLY draw with original data
ggbarplot(df2, x = "cyl", y = "n_cyl",
          width = 0.7, fill = "steelblue", color = NA,
          ggtheme = theme_minimal())

### 2.2.1. Sorted by Y variable (number)
# Ascending (Y axis)
ggplot(mtcars, aes(x = reorder(factor(cyl), cyl, sum))) + # reorder()
    geom_bar(stat = "count", width = 0.7, fill = "steelblue") +
    theme_minimal()
ggbarplot(df2, x = "cyl", y = "n_cyl",
          width = 0.7, fill = "steelblue", color = NA,
          sort.val = "asc",     # sorted setting
          sort.by.groups = FALSE,
          ggtheme = theme_minimal())

# Descending (Y axis)
ggplot(mtcars, aes(x = reorder(factor(cyl), -cyl, sum))) +
    geom_bar(stat = "count", width = 0.7, fill = "steelblue") +
    theme_minimal()
ggbarplot(df2, x = "cyl", y = "n_cyl",
          width = 0.7, fill = "steelblue", color = NA,
          sort.val = "desc", 
          ggtheme = theme_minimal())

### 2.2.2. Draw barplot with propertion data
ggplot(mtcars, aes(x = factor(cyl), y = ..prop.., group = 1)) + # ..prop..
    geom_bar(width = 0.7, fill = "steelblue") +
    theme_minimal()

df3 <- mtcars %>% group_by(cyl) %>% 
    summarise(n_cyl = n(), p_cyl = n() / dim(mtcars)[1])
df3$cyl <- as.factor(df3$cyl)
ggbarplot(df3, x = "cyl", y = "p_cyl",
          width = 0.7, fill = "steelblue", color = NA,
          ggtheme = theme_minimal())

## 2.3. Using color
### 2.3.1. Line color
p <- ggplot(df, aes(dose, len, color = dose)) +
    geom_bar(stat = "identity", fill = "white")
p

# Use custom color palettes [scale_color_manual()]
p + scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))
ggbarplot(df, x = "dose", y = "len", color = "dose",
          palette = c("#999999", "#E69F00", "#56B4E9"),
          ggtheme = theme_grey()) 

# Use brewer color palettes [scale_color_brewer()]
p + scale_color_brewer(palette = "Dark2")
ggbarplot(df, x = "dose", y = "len", color = "dose",
          palette = "Dark2", # setting palette
          ggtheme = theme_grey())

# Use grey scale
p + scale_color_grey() + theme_classic()

### 2.3.2. Fill color
p <- ggplot(df, aes(x = dose, y = len, fill = dose)) +
    geom_bar(stat = "identity") + theme_minimal()
p

ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = NA,
          ggtheme = theme_minimal())

# Use custom color palettes
p + scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))
ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = NA,
          palette = c("#999999", "#E69F00", "#56B4E9"),
          ggtheme = theme_minimal())

# Use brewer color palettes
p + scale_fill_brewer(palette = "Dark2")
ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = NA,
          palette = "Dark2",
          ggtheme = theme_minimal())

# Use grey scale
p + scale_fill_grey()

# Use black outline color
ggplot(df, aes(x = dose, y = len, fill = dose)) +
    geom_bar(stat = "identity", color = "black") +
    scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9")) +
    theme_minimal()
ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = "black",
          palette = c("#999999", "#E69F00", "#56B4E9"),
          ggtheme = theme_minimal())

## 2.4. Legend
p <- p + scale_fill_brewer(palette = "Blues")
p + theme(legend.position = "top")

ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = NA,
          palette = "Blues",
          legend = "top",  # 'bottom', 'right', 'left', 'none'
          ggtheme = theme_minimal())

p + theme(legend.position = "bottom")
p + theme(legend.position = "none")  # remove legend

## 2.5. Sorted by X variable
p + scale_x_discrete(limits = c("D2", "D0.5", "D1"))
ggbarplot(df, x = "dose", y = "len",
          fill = "dose", color = NA,
          palette = "Blues",
          order = c("D2", "D0.5", "D1"),
          ggtheme = theme_minimal())

## 2.6. Multi-group barplot (discrete X, continous Y, discrete group variable)
# The demo data have 3 columns
df2 <- data.frame(
    supp = rep(c("VC", "OJ"), each = 3),  # Grouped var
    dose = rep(c("D0.5", "D1", "D2"), 2), # X axis
    len = c(6.8, 15, 33, 4.2, 10, 29.5)   # Y axis
)
head(df2)

# stacked bar
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity")
ggbarplot(df2, x = "dose", y = "len", 
          fill = "supp", color = NA,
          ggtheme = theme_gray())

# grouped bar (position_dodge)
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = "dodge")
ggbarplot(df2, x = "dose", y = "len", 
          fill = "supp", color = NA,
          position = position_dodge(0.75),
          ggtheme = theme_gray())

# Standardized by grouped variables
# All columns have same height (=1)
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = "fill")

ggbarplot(df2, x = "dose", y = "len", 
          fill = "supp", color = NA,
          position = position_fill(),
          ggtheme = theme_gray())

# stacked barplot
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = "stack")
ggbarplot(df2, x = "dose", y = "len", 
          fill = "supp", color = NA,
          position = position_stack(),
          ggtheme = theme_gray())

# The 'width' parameter is used to set the overlop of bars
#   in position_dodge() function
#   'width = 1' => "dodge"
#   'width = 0' => "stack"
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = position_dodge(width = 1))
ggbarplot(df2, x = "dose", y = "len",
          fill = "supp", color = NA,
          position = position_dodge(width = 1),
          ggtheme = theme_gray())

# set color manually
p <- ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", color = "black", position = position_dodge())

# Use custom colors
p + scale_fill_manual(values = c('#999999', '#E69F00'))
ggbarplot(df2, x = "dose", y = "len",
          fill = "supp", color = "black",
          palette = c('#999999', '#E69F00'),
          position = position_dodge())

# Use brewer color palettes
p + scale_fill_brewer(palette = "Blues")
ggbarplot(df2, x = "dose", y = "len",
          fill = "supp", color = "black",
          palette = "Blues",
          position = position_dodge())

# add labels on dodged barplot
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_text(aes(label = len), vjust = 1.6, color = "black",
              position = position_dodge(0.9), size = 3.5) +
    scale_fill_brewer(palette = "Paried") +
    theme_minimal()
ggbarplot(df2, x = "dose", y = "len",
          fill = "supp", color = NA,
          position = position_dodge(),
          label = "len", lab.vjust = 1.6, lab.col = "black", lab.size = 3.5,
          lab.pos = "in",
          palette = "Paired", ggtheme = theme_minimal())

# add labels on stacked barplot
df_sorted <- arrange(df2, dose, supp)
head(df_sorted)
df_cumsum <- df_sorted %>% group_by(dose) %>% 
    mutate(lab_ypos = cumsum(len)) # calculate the cumulated 'len' per dose

ggplot(df_cumsum, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity") +
    geom_text(aes(y = lab_ypos, label = len), vjust = 1.6,
              color = "black", size = 3.5) +
    scale_fill_brewer(palette = "Paired") +
    theme_minimal()
ggbarplot(df_cumsum, x = "dose", y = "len",
          fill = "supp", palette = "Paired", color = NA,
          label = "len", lab.pos = "out", lab.size = 3.5, lab.vjust = 1.6,
          ggtheme = theme_minimal())

# locate label between bars
df_cumsum2 <- df_sorted %>% group_by(dose) %>% 
    mutate(lab_ypos = cumsum(len) - len / 2)
ggplot(df_cumsum2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity") +
    geom_text(aes(y = lab_ypos, label = len), vjust = 1.6,
              color = "black", size = 3.5) +
    scale_fill_brewer(palette = "Paired") +
    theme_minimal()

## 2.7. Numerous X axis
df2 <- data.frame(
    supp = rep(c("VC", "OJ"), each = 3),
    dose = rep(c("0.5", "1", "2"), 2),
    len = c(6.8, 15, 33, 4.2, 10, 29.5)
)
head(df2)

# X variable is a continous variable
df2$dose <- as.numeric(as.vector(df2$dose))
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    scale_fill_brewer(palette = "Paired") +
    theme_minimal()

# X axis is a discrete variable
df2$dose <- as.factor(df2$dose)
ggplot(df2, aes(dose, len, fill = supp)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    scale_fill_brewer(palette = "Paired") +
    theme_minimal()
ggbarplot(df2, x = "dose", y = "len",
          fill = "supp", color = NA,
          position = position_dodge(0.7),
          palette = "Paired",
          ggtheme = theme_minimal())

## 2.8. Errorbar
# Calculate SD by grouped variable
# MEAN and SD should be calculated for ggplot2 errorbar
df3 <- ToothGrowth %>% group_by(supp, dose) %>% 
    mutate(mean = mean(len), sd = sd(len))

p <- ggplot(df3, aes(x = dose, y = mean, fill = supp)) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),
                  width = 0.1,
                  position = position_dodge(0.45))
# It's diffcult that position of errorbar align with bar
p + scale_fill_brewer(palette = "Paired") + theme_minimal()

# ggpubr can calculate errorbar data automatically
ggbarplot(ToothGrowth, x = "dose", y = "len", 
          fill = "supp", position = position_dodge(0.7),
          palette = "Paired", color = NA,
          ggtheme = theme_minimal(),
          add = "mean_sd", 
          add.params = list(color = "supp"),
          error.plot = "errorbar")
# For un-grouped data, [add = "mean"] can add errobar
# For grouped data, "add.parmas" also should be setted; 
#       otherwise only one errorbar plotted.

# Set barplot's properties
p + labs(title = "Plot of length per dose",
         x = "Dose (mg)", y = "Length") +
    scale_fill_manual(values = c("black", "lightgray")) +
    theme_classic()
ggbarplot(ToothGrowth, x = "dose", y = "len", 
          fill = "supp", position = position_dodge(0.7),
          palette = c("black", "lightgray"), color = NA,
          ggtheme = theme_classic(),
          title = "Plot of length per dose",
          xlab = "Dose (mg)", ylab = "Length",
          add = "mean_sd", 
          add.params = list(color = "supp"),
          error.plot = "errorbar")

# green
p + scale_fill_brewer(palette = "Greens") + theme_minimal()
ggbarplot(ToothGrowth, x = "dose", y = "len", 
          fill = "supp", position = position_dodge(0.7),
          palette = "Greens", color = NA,
          ggtheme = theme_minimal(),
          title = "Plot of length per dose",
          xlab = "Dose (mg)", ylab = "Length",
          add = "mean_sd", 
          add.params = list(color = "supp"),
          error.plot = "errorbar")

# red
p + scale_fill_brewer(palette = "Reds") + theme_minimal()
ggbarplot(ToothGrowth, x = "dose", y = "len", 
          fill = "supp", position = position_dodge(0.7),
          palette = "Reds", color = NA,
          ggtheme = theme_minimal(),
          title = "Plot of length per dose",
          xlab = "Dose (mg)", ylab = "Length",
          add = "mean_sd", 
          add.params = list(color = "supp"),
          error.plot = "errorbar")

# ggbarplot with errorbar
df <- ToothGrowth

# It can be seen that for each group we have different values
ggbarplot(df, x = "dose", y = "len")

# Visualize the mean of each group
ggbarplot(df, x = "dose", y = "len", add = "mean")

# Add error bar (mean_se)
#   mean_sd, mean_ci, mean_iqr ...
ggbarplot(df, x = "dose", y = "len",
          add = "mean_se", label = TRUE, lab.vjust = -1.6)

# Upper_errrorbar
ggbarplot(df, x = "dose", y = "len",
          add = "mean_se", error.plot = "upper_errorbar")

# pointrange
ggbarplot(df, x = "dose", y = "len",
          add = c("mean_se", "jitter"))

# Add dot and errors (mean_se)
ggbarplot(df, x = "dose", y = "len",
          add = c("mean_se", "dotplot"))

# Grouped error
ggbarplot(df, x = "dose", y = "len", coloc = "supp",
          add = "mean_se", palette = c("#00AFBB", "#E7B800"),
          position = position_dodge())


# barplot and line plot (one grouped variable)
ggbarplot(ToothGrowth, x = "dose", y = "len",
          add = "mean_se") +
    stat_compare_means() +
    stat_compare_means(ref.group = "0.5",
                       label = "p.signif", label.y = c(22, 29))

# Line plot with errorbar
ggline(ToothGrowth, x="dose", y="len", add = "mean_se")+
    stat_compare_means()+ 
    stat_compare_means(ref.group = "0.5", label = "p.signif", label.y = c(22, 29))

# barplot and line plot (two grouped variables)
ggbarplot(ToothGrowth, x = "dose", y = "len",
    add = "mean_se", color = "supp",
    palette = "jco", position = position_dodge(0.8)) +
    stat_compare_means(aes(group = supp), label = "p.signif", label.y = 29)

ggline(
    ToothGrowth,
    x = "dose",
    y = "len",
    add = "mean_se",
    color = "supp",
    palette = "jco"
) +
    stat_compare_means(aes(group = supp),
                       label = "p.signif",
                       label.y = c(16, 25, 29))

## 2.9. Test and p-value
# Barplot and line plot (one grouped variable)
ggbarplot(ToothGrowth, x = "dose", y = "len", add = "mean_se",
          palette = "jco") +
    stat_compare_means() +
    stat_compare_means(ref.group = "0.5",
                       label = "p.signif", label.y = c(22, 29))

cmps <- list(c("0.5", "1"), c("1", "2"),c("0.5", "2"))
ggbarplot(ToothGrowth, x = "dose", y = "len", fill = "dose", add = "mean_se",
          palette = "jco") +
    stat_compare_means(comparisons = cmps) +
    stat_compare_means(label.y = 50)


# 3. Bar plot and theme setting
data <- data.frame(
    #Name = c("Apple", "Google", "Facebook", "Amazon", "Tencent"),
    Company = c("Apple", "Google", "Facebook", "Amazon", "Tencent"),
    Sale2015 = c(5000, 3500, 2300, 2100, 3100),
    Sale2016 = c(5050, 3800, 2900, 2500, 3300))

# Transfer wide data to long data
mydata <- gather(data, key = "Year", value = "Sale", Sale2015, Sale2016)
# mydata <- melt(data, id.vars = "Company", 
#                variable.name = "Year", value.name = "Sale")

# dodge arrange
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge')

# set theme
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj()

# set theme 2
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj('rgby', '')

# set theme 3
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj('rgby', '') +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

# set theme 4
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ggtitle('The Financial Performance of Top 5') +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

# stack arrange
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'stack') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj('rgby', '') +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

# set theme
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'stack') +
  ggtitle('The Financial Performance of Top 5') +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'fill') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj('rgby', '') +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'fill') +
  ggtitle('The Financial Performance of Top 5') +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL))

# Horizontal barplot
ggplot(mydata, aes(Company, Sale, fill = Year)) + 
  geom_bar(stat = 'identity', position = 'dodge') +
  ggtitle('The Financial Performance of Top 5') +
  theme_wsj() +
  scale_fill_wsj('rgby', '') +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  coord_flip()  # rotate


# 4. Grouped bars
library(ggplot2)
library(ggthemes)
library(reshape2)

# Data----
data <- data.frame(
    #Name = c("Apple", "Google", "Facebook", "Amozon", "Tencent"),
    Company = c("Apple", "Google", "Facebook", "Amozon", "Tencent"),
    Sale2013 = c(5000, 3500, 2300, 2100, 3100),
    Sale2014 = c(5050, 3800, 2900, 2500, 3300),
    Sale2015 = c(5050, 3800, 2900, 2500, 3300),
    Sale2016 = c(5050, 3800, 2900, 2500, 3300))

mydata <- melt(data, id.vars = "Company",
    variable.name = "Year", value.name = "Sale")

# Basic ggplot object
p0 <- ggplot(mydata, aes(Company, Sale, fill = Year)) +
  geom_bar(stat = 'identity', position = 'dodge')

p <- p0 +
  theme(axis.ticks.length = unit(0.5, 'cm')) +
  guides(fill = guide_legend(title = NULL)) +
  ggtitle('Performance of Top5') +
  theme(axis.title = element_blank())

# Special theme
p + theme_wsj() + scale_fill_wsj('rgby', '')

p + theme_economist() + scale_fill_economist()

# facet
pf <- p + facet_grid(. ~ Year)

pf + theme_wsj() + scale_fill_wsj('rgby', '')

pf + theme_economist() + scale_fill_economist()

# Horizontaol (coord_flip)
pf + theme_wsj() + scale_fill_wsj('rgby', '') + coord_flip()

pf + theme_economist() + scale_fill_economist() + coord_flip()

# Vertical (coord_flip)
pfv <- p + facet_grid(Year ~ .)
pfv + theme_wsj() + scale_fill_wsj('rgby', '')
pfv + theme_economist() + scale_fill_economist()

## Labels
# ? why error?
# p + geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

q <- ggplot(mydata,aes(Company,Sale,fill=Year,label =Sale))
  geom_bar(stat="identity",position="dodge")+
  theme_wsj()+
  scale_fill_wsj("rgby", "")+
  theme(axis.ticks.length=unit(0.5,'cm'))+
  guides(fill=guide_legend(title=NULL))+
  ggtitle("The Financial Performance of Five Giant")+
  theme(axis.title = element_blank())
q <- geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

# facet labels
ggplot(mydata,aes(Company,Sale,fill=Year,label =Sale))+
  geom_bar(stat="identity",position="dodge")+
  theme(axis.ticks.length=unit(0.5,'cm'))+
  guides(fill=guide_legend(title=NULL))+
  ggtitle("The Financial Performance of Five Giant")+
  theme(axis.title = element_blank(),legend.position='none')+ 
  facet_grid(.~Year)+
  theme_wsj()+
  scale_fill_wsj("rgby", "")+
  geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)

pfv + theme_wsj() + scale_fill_wsj('rgby', '') +
  geom_text(aes(y = Sale + 0.05, label = Sale), position = position_dodge(0.9), vjust = -0.5)
